package com.netflexitysolutions.amazonws.ec2.internal.operations;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.amazonaws.ec2.doc._2009_04_04.DescribeReservedInstancesOfferingsResponseSetItemType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeReservedInstancesOfferingsResponseSetType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeReservedInstancesOfferingsResponseType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeReservedInstancesOfferingsSetItemType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeReservedInstancesOfferingsSetType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeReservedInstancesOfferingsType;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.InstanceType;
import com.netflexitysolutions.amazonws.ec2.ReservedInstancesOffering;

public class DescribeReservedInstancesOfferingsOperation extends
		ObjectSetOperationExecutor<DescribeReservedInstancesOfferingsType, List<ReservedInstancesOffering>> {
	
	private InstanceType instanceType;
	
	private String availabilityZone;
	
	private String productDescription;

	public DescribeReservedInstancesOfferingsOperation(EC2 ec2, Set<String> objectIds, InstanceType instanceType, String availabilityZone, String productDescription) {
		super(ec2, objectIds);
		this.instanceType = instanceType;
		this.availabilityZone = availabilityZone;
		this.productDescription = productDescription;
	}

	@Override
	protected List<ReservedInstancesOffering> call(DescribeReservedInstancesOfferingsType request) {
		request.setAvailabilityZone(availabilityZone);
		request.setProductDescription(productDescription);
		request.setInstanceType(instanceType != null ? instanceType.getName() : null);
		if (objectIds != null) {
			DescribeReservedInstancesOfferingsSetType set = new DescribeReservedInstancesOfferingsSetType();
			List<DescribeReservedInstancesOfferingsSetItemType> list = set.getItem();
			for (String instanceId : objectIds) {
				DescribeReservedInstancesOfferingsSetItemType item = new DescribeReservedInstancesOfferingsSetItemType();
				item.setReservedInstancesOfferingId(instanceId);
				list.add(item);
			}
			request.setReservedInstancesOfferingsRequestSet(set);
		}
		DescribeReservedInstancesOfferingsResponseType response = getEC2().getService().describeReservedInstancesOfferings(request);
		return toDomainType(response.getReservedInstancesOfferingsResponseSet());
	}
	
	protected List<ReservedInstancesOffering> toDomainType(DescribeReservedInstancesOfferingsResponseSetType offerings) {
		if(offerings == null) {
			return null;
		}
		
		List<ReservedInstancesOffering> result = new ArrayList<ReservedInstancesOffering>();
		List<DescribeReservedInstancesOfferingsResponseSetItemType> list = offerings.getItem();
		for (DescribeReservedInstancesOfferingsResponseSetItemType item : list) {
			result.add(toDomainType(item));
		}
		return result;
	}
	
	protected ReservedInstancesOffering toDomainType(DescribeReservedInstancesOfferingsResponseSetItemType offering) {
		if(offering == null) {
			return null;
		}
		
		ReservedInstancesOffering result = new ReservedInstancesOffering();
		result.setReservedInstancesOfferingId(offering.getReservedInstancesOfferingId());
		result.setAvailabilityZone(offering.getAvailabilityZone());
		result.setDuration(offering.getDuration());
		result.setFixedPrice(offering.getFixedPrice());
		result.setInstanceType(InstanceType.instanceTypeForName(offering.getInstanceType()));
		result.setProductDescription(offering.getProductDescription());
		result.setUsagePrice(offering.getUsagePrice());
		
		return result;
	}
	
}
